<?php
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH',
              realpath(dirname(dirname(__FILE__)) . '/application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV',
              (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
                                         : 'production'));
//error_reporting(E_ERROR);
// Typically, you will also want to add your library/ directory
// to the include_path, particularly if it contains your ZF installed
set_include_path(implode(PATH_SEPARATOR, array(
    dirname(dirname(__FILE__)) . '/library',
    get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';

//$autoloader->setFallbackAutoloader('true');
$environment = APPLICATION_ENV;
$options = APPLICATION_PATH . '/configs/application.ini';
$application = new Zend_Application($environment, $options);

require_once APPLICATION_PATH.'/definitions/Definitions.php';
require_once 'Zend/Controller/Plugin/Abstract.php';
require_once 'Zend/Controller/Front.php';

/**
 * Plugin for ACL
 */
class ACLPlugin extends Zend_Controller_Plugin_Abstract {

	/**
	 * Called before an action is dispatched by Zend_Controller_Dispatcher.
	 *
	 * @param  Zend_Controller_Request_Abstract $request
	 * @return void
	 */
	public function preDispatch(Zend_Controller_Request_Abstract $request)
	{
		$this->_doActionRelationAccessCheck($request);

	}

	/**
	 * Action : call control files
	 */
	private function _doActionRelationAccessCheck(Zend_Controller_Request_Abstract $request) {

		$acl = array(
			//Front: register member
			array('action'=>'registrationconfirm','pre_mode'=>'registrationinput',   'redirect'=>'registrationbenefit','controller'=>'registration'),
			array('action'=>'registrationfinish', 'pre_mode'=>'registrationconfirm', 'redirect'=>'registrationbenefit','controller'=>'registration'),

		);

		$moduleName = strtolower($request->getModuleName());
		$controlerName = strtolower($request->getControllerName());
		$actionName = strtolower($request->getActionName());

		
		$preMode = $request->getParam('modename','');

		if ($actionName == $preMode) return;

		foreach ($acl as $key=>$action){

			if ($actionName == $action['action']) {

				if($action['pre_mode'] != $preMode){
					//redirect action
					$request->setControllerName($action['controller']);
					$request->setActionName($action['redirect']);
				}
				break;
			}
		}
	}
}

//Registe ACLPlugin
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new ACLPlugin());
$front->addModuleDirectory(APPLICATION_PATH. "/widgets") ;

$application->bootstrap()->run();




